package com.dfrz.stockMarket.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dfrz.stockMarket.bean.UserBean;
import com.dfrz.stockMarket.cons.Constants;
import com.dfrz.stockMarket.model.StocksModel;
import com.dfrz.stockMarket.model.UserStocksModel;
import com.dfrz.stockMarket.service.StockService;
import com.dfrz.stockMarket.service.UserService;

/**
 * 用户加入自选操作
 * @author Happyniko
 *
 */
public class UserAddCollection extends BaseController{
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("开心");
		Integer stockCode = Integer.parseInt(req.getParameter("stockCode"));
		System.out.println("stockCode---"+stockCode);
		UserBean userBean = getSessionUserBean(req);
		int userid=userBean.getUserInfoModel().getUserID();
	/*	System.out.println("user"+userBean);*/
		StockService stockservice =new StockService();
		
		UserStocksModel userStocksModel1=null;
		try {
			
			List<StocksModel> list= (List<StocksModel>) stockservice.getAllUserStockCode(userid).getMsg();
			Boolean flag=true;
			for(Object obj:list){
				int sto=((UserStocksModel)obj).getStockCode();
				System.out.println("sql.getstockcode--"+sto);
				System.out.println("stockcode--"+stockCode);
				
				if(sto==stockCode){
					flag=false;
				}
			}
			if(flag){
				StocksModel stockmodel= (StocksModel) stockservice.getStockByID(stockCode).getMsg();
				userStocksModel1 = new UserStocksModel(userid,stockCode,stockmodel.getStockName(), stockmodel.getStockPrice());
				UserService service = new UserService();
				try {
					System.out.println("userStocksModel1---"+userStocksModel1);
					service.saveUserStocksModel(userStocksModel1);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	
	}
	
}
